﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.Entity;
using BotonJXC.BLL;
using BotonJXC.Util;

namespace BotonJXC.Forms
{
    public partial class PickingForm : BaseMdiForm
    {
        private int pageNo;
        private Pickings pickings;
        public PickingForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 上一张单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void upPageButton_Click(object sender, EventArgs e)
        {
            loadData(pageNo -1);
        }
        /// <summary>
        /// 下一张单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nextPageButton_Click(object sender, EventArgs e)
        {
            loadData(pageNo+1);
        }

        /// <summary>
        /// 查找数据
        /// </summary>
        /// <param name="start"></param>
        public void loadData(int start)
        {
            PageModel<Pickings> pageModellist = new PickingsBLL().FindPage(null, start, 1);
            int pageCount = pageModellist.PageCount;//总页数
            pageNo = start;//当前页数
            if (pageNo == 0)
                upPageButton.Enabled = false;
            else
                upPageButton.Enabled = true;
            if ((pageNo + 1) == pageCount)
                nextPageButton.Enabled = false;
            else
                nextPageButton.Enabled = true;

            if (pageModellist.Results.Count > 0)
            {
                pickings = pageModellist.Results[0];
                initFormData();
            }
        }


        /// <summary>
        /// 初始化窗体数据
        /// </summary>
        public void initFormData()
        {
         
            if (pickings != null)
            {
                this.pickingSn.Text = "单据编号: " + pickings.PickingSn;//单据编号
                //SalesmanText.Text = "业务员：" + pickings.Salesman;//业务员

                if ("1".Equals(pickings.OutBillType))
                {
                    this.orderType.Text = "相关单据类型:　配送中心调拨单";
                    if (pickings.Allocates != null) {
                        this.orderSN.Text = "相关单号: " + pickings.Allocates.AllocateSn;//相关单据类型
                    }
                }
                else {
                    this.orderType.Text = "相关单据类型:　配送中心配送出库";
                    if (pickings.OutStocks != null)
                    {
                        this.orderSN.Text ="相关单号: " + pickings.OutStocks.OutSn;//相关单据类型
                    }

                   
                }
        
                AccountText.Text = "制单人：" + pickings.Account.Username;//制单人
                CreateTimeText.Text = "制单日期：" + string.Format("{0:yyyy/MM/dd HH:mm:ss}", pickings.CreateTime);//创建时间

                remark.Text ="备注: "+ pickings.Remark;//通知备注

                PrintTimeText.Text = "打印次数：" + pickings.PrintTime;//打印次数
                this.outWarehouse.Text = "配送出库库存地: " + pickings.OutWarehouse.Name;
                this.inWarehouse.Text = "配送入库库存地: " + pickings.InWarehouse.Name;

               // OutWarehousecb.SelectedValue = pickings.OutWarehouse.WarehouseId;//出库库存地
                //OutWarehousetb.Text = Utils.ToString(pickings.OutWarehouse.WarehouseId);

                //InWarehousecb.SelectedValue = pickings.InWarehouse.WarehouseId;//入库库存地
                //InWarehousetb.Text = Utils.ToString(pickings.InWarehouse.WarehouseId);
                //货品集合
                BindingList<PickingsProducts> list = new BindingList<PickingsProducts>(pickings.PickingsProductsList);
                PickinguserDataGridViewProduct.DataSource = list;
                PickinguserDataGridViewProduct.Refresh();
            }

        }

        private void PickingForm_Load(object sender, EventArgs e)
        {
            loadData(0);
        }

        /// <summary>
        /// 单元格格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PickinguserDataGridViewProduct_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            PickingsProducts p = PickinguserDataGridViewProduct.Rows[e.RowIndex].DataBoundItem as PickingsProducts;
            if (p != null && p.Product != null)
            {
                if (PickinguserDataGridViewProduct.Columns[e.ColumnIndex].Name == "Barcode")
                {
                    e.Value = p.Product.Barcode;
                }
                else if (PickinguserDataGridViewProduct.Columns[e.ColumnIndex].Name == "ProductCode")
                {
                    e.Value = p.Product.ProductCode;
                }
                else if (PickinguserDataGridViewProduct.Columns[e.ColumnIndex].Name == "ProductName")
                {
                    e.Value = p.Product.ProductName;
                }
                else if (PickinguserDataGridViewProduct.Columns[e.ColumnIndex].Name == "DefUnit")
                {
                    if (p.Product.DefUnit != null)
                        e.Value = p.Product.DefUnit.UnitName;
                }
                else if (PickinguserDataGridViewProduct.Columns[e.ColumnIndex].Name == "CargoSpace")
                {
                    e.Value = p.Product.CargoSpace;
                }
                else if (PickinguserDataGridViewProduct.Columns[e.ColumnIndex].Name == "productType")
                {
                    if (p.Product.Type != null)
                        e.Value = p.Product.Type.TypeName;
                }
            }
        }

    }
}
